<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="600" append-to-body destroy-on-close @closed="$emit('closed')">
		<el-container>
			<el-main class="nopadding">
				<el-scrollbar>
					<el-row class="drawer-table">
						<el-col :span="24">
							<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="80px">
								<el-form-item label="开票信息" prop="invoiceId">
									<el-input v-model="form.invoiceId" placeholder="请输入开票信息" clearable></el-input>
								</el-form-item>
								<el-form-item label="企业" prop="departmentId">
									<el-input v-model="form.departmentId" placeholder="请输入企业" clearable></el-input>
								</el-form-item>
								<el-form-item label="1支付发票 2收款发票 1对方开给我方的   2我方开给对方的" prop="type">
									<el-input v-model="form.type" placeholder="请输入1支付发票 2收款发票 1对方开给我方的   2我方开给对方的" clearable></el-input>
								</el-form-item>
								<el-form-item label="关联业务类型" prop="refType">
									<el-input v-model="form.refType" placeholder="请输入关联业务类型" clearable></el-input>
								</el-form-item>
								<el-form-item label="关联业务id" prop="refId">
									<el-input v-model="form.refId" placeholder="请输入关联业务id" clearable></el-input>
								</el-form-item>
								<el-form-item label="企业名称--买方" prop="buyerName">
									<el-input v-model="form.buyerName" placeholder="请输入企业名称--买方" clearable></el-input>
								</el-form-item>
								<el-form-item label="纳税人识别号" prop="buyerCode">
									<el-input v-model="form.buyerCode" placeholder="请输入纳税人识别号" clearable></el-input>
								</el-form-item>
								<el-form-item label="电话" prop="buyerTelephone">
									<el-input v-model="form.buyerTelephone" placeholder="请输入电话" clearable></el-input>
								</el-form-item>
								<el-form-item label="企业地址" prop="buyerAddress">
									<el-input v-model="form.buyerAddress" placeholder="请输入企业地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="开户银行的名称" prop="buyerBankName">
									<el-input v-model="form.buyerBankName" placeholder="请输入开户银行的名称" clearable></el-input>
								</el-form-item>
								<el-form-item label="银行账号" prop="buyerBankAccount">
									<el-input v-model="form.buyerBankAccount" placeholder="请输入银行账号" clearable></el-input>
								</el-form-item>
								<el-form-item label="状态1待生效  2待支付  3已支付" prop="status">
									<el-input v-model="form.status" placeholder="请输入状态1待生效  2待支付  3已支付" clearable></el-input>
								</el-form-item>
								<el-form-item label="总金额" prop="totalAmount">
									<el-input v-model="form.totalAmount" placeholder="请输入总金额" clearable></el-input>
								</el-form-item>
								<el-form-item label="税率%" prop="taxRate">
									<el-input v-model="form.taxRate" placeholder="请输入税率%" clearable></el-input>
								</el-form-item>
								<el-form-item label="企业名称--销售方" prop="sellerName">
									<el-input v-model="form.sellerName" placeholder="请输入企业名称--销售方" clearable></el-input>
								</el-form-item>
								<el-form-item label="纳税人识别号" prop="sellerCode">
									<el-input v-model="form.sellerCode" placeholder="请输入纳税人识别号" clearable></el-input>
								</el-form-item>
								<el-form-item label="电话" prop="sellerTelephone">
									<el-input v-model="form.sellerTelephone" placeholder="请输入电话" clearable></el-input>
								</el-form-item>
								<el-form-item label="企业地址" prop="sellerAddress">
									<el-input v-model="form.sellerAddress" placeholder="请输入企业地址" clearable></el-input>
								</el-form-item>
								<el-form-item label="开户银行的名称" prop="sellerBankName">
									<el-input v-model="form.sellerBankName" placeholder="请输入开户银行的名称" clearable></el-input>
								</el-form-item>
								<el-form-item label="银行账号" prop="sellerBankAccount">
									<el-input v-model="form.sellerBankAccount" placeholder="请输入银行账号" clearable></el-input>
								</el-form-item>
								<el-form-item label="开票人" prop="createId">
									<el-input v-model="form.createId" placeholder="请输入开票人" clearable></el-input>
								</el-form-item>
								<el-form-item label="开票时间" prop="createTime">
									<el-input v-model="form.createTime" placeholder="请输入开票时间" clearable></el-input>
								</el-form-item>
								<el-form-item label="图片" prop="url">
									<el-input v-model="form.url" placeholder="请输入图片" clearable></el-input>
								</el-form-item>
								<el-form-item label="说明" prop="msg">
									<el-input v-model="form.msg" placeholder="请输入说明" clearable></el-input>
								</el-form-item>
                            </el-form>
						</el-col>
					</el-row>
				</el-scrollbar>
			</el-main>
			<el-footer>
				<el-button v-if="mode!=='show'" :loading="isSaveing" @click="submit()" type="primary" size="small">保 存</el-button>
				<el-button size="small" @click="visible=false">取 消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>
<script>
	import financeAPI from '@/api/finance/invoice';
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					mode: '发票，开票记录'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					invoiceId: "",
					departmentId: "",
					type: "",
					refType: "",
					refId: "",
					buyerName: "",
					buyerCode: "",
					buyerTelephone: "",
					buyerAddress: "",
					buyerBankName: "",
					buyerBankAccount: "",
					status: "",
					totalAmount: "",
					taxRate: "",
					sellerName: "",
					sellerCode: "",
					sellerTelephone: "",
					sellerAddress: "",
					sellerBankName: "",
					sellerBankAccount: "",
					createId: "",
					createTime: "",
					url: "",
					msg: "",
				},
				//验证规则
				rules: {
					userName: [
						{required: true, message: '请输入名称'}
					],
					userAge: [
						{pattern: /^[0-9]*$/, message: '年龄需为数字', trigger: 'blur'}
					],
					price: [
						{pattern: /^\d+(\.\d+)?$/, message: '价格需为数字', trigger: 'blur'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if(valid){
						this.isSaveing = true;
						var res = await financeAPI.invoice.save.post(this.form);
						this.isSaveing = false;
						if(res.code === 2000){
							this.$emit('success', this.form, this.mode);
							this.visible = false;
							this.$message.success(res.message);
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				});
			},
			//表单注入数据
			setData(data){
				this.form.invoiceId = data.invoiceId;
				this.form.departmentId = data.departmentId;
				this.form.type = data.type;
				this.form.refType = data.refType;
				this.form.refId = data.refId;
				this.form.buyerName = data.buyerName;
				this.form.buyerCode = data.buyerCode;
				this.form.buyerTelephone = data.buyerTelephone;
				this.form.buyerAddress = data.buyerAddress;
				this.form.buyerBankName = data.buyerBankName;
				this.form.buyerBankAccount = data.buyerBankAccount;
				this.form.status = data.status;
				this.form.totalAmount = data.totalAmount;
				this.form.taxRate = data.taxRate;
				this.form.sellerName = data.sellerName;
				this.form.sellerCode = data.sellerCode;
				this.form.sellerTelephone = data.sellerTelephone;
				this.form.sellerAddress = data.sellerAddress;
				this.form.sellerBankName = data.sellerBankName;
				this.form.sellerBankAccount = data.sellerBankAccount;
				this.form.createId = data.createId;
				this.form.createTime = data.createTime;
				this.form.url = data.url;
				this.form.msg = data.msg;
			}
		}
	}
</script>
<style>
</style>
